{
 "metadata": {
  "name": "",
  "signature": "sha256:f0ab5b537b50495c568c51c18ad1f9280278dbb2fc1137b985b05c1ad4b42880"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import os\n",
      "import subprocess\n",
      "import re\n",
      "\n",
      "def subshell(cmd):\n",
      "    return subprocess.check_output(cmd, shell=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def get_pwd_workspace_name():\n",
      "    workspace = subshell(\"bazel info workspace\").strip()\n",
      "    workspace_file = os.path.join(workspace, 'WORKSPACE')\n",
      "\n",
      "    text = open(workspace_file).read()\n",
      "\n",
      "    # Find workspace() statement\n",
      "    m = re.search(r\"workspace\\s*\\((.*?)\\)\", text, re.M)\n",
      "    # Evaluate method to recover kwargs\n",
      "    workspace_info = eval(m.group(0), {\"workspace\": dict})\n",
      "    # Extract name\n",
      "    workspace_name = workspace_info['name']\n",
      "    return workspace_name"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print get_pwd_workspace_name()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "repro\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}